package com.hutiao.snowhb.actions.admin;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.resource.ConfigInfo;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/admin")
public class SystemInfoAction extends ActionSupport {

	private static final long serialVersionUID = 8952926938118560548L;

	private String wxAppId;
	
	private String wxSecret;
	
	private String wxAccessToken;
	
	private int wxTokenExpiresIn;
	
	private int wxTokenExcessTime;
	
	private String accessToken;
	
	public String getWxAppId() {
		return wxAppId;
	}

	public void setWxAppId(String wxAppId) {
		this.wxAppId = wxAppId;
	}

	public String getWxSecret() {
		return wxSecret;
	}

	public void setWxSecret(String wxSecret) {
		this.wxSecret = wxSecret;
	}

	public String getWxAccessToken() {
		return wxAccessToken;
	}

	public void setWxAccessToken(String wxAccessToken) {
		this.wxAccessToken = wxAccessToken;
	}

	public int getWxTokenExpiresIn() {
		return wxTokenExpiresIn;
	}

	public void setWxTokenExpiresIn(int wxTokenExpiresIn) {
		this.wxTokenExpiresIn = wxTokenExpiresIn;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public int getWxTokenExcessTime() {
		return wxTokenExcessTime;
	}

	public void setWxTokenExcessTime(int wxTokenExcessTime) {
		this.wxTokenExcessTime = wxTokenExcessTime;
	}

	@Action(value="systeminfo")
	public String getSystemInfo() {
		this.setValue();
		return SUCCESS;
	}
	
	@Action(value="reset_token",results = {@Result(name="success", type="chain", location="systeminfo")})
	public String resetAccessToken() throws Exception {
		WeixinUtil.getNewAccessToken();
		this.setValue();
		return SUCCESS;
	}
	
	public void setValue() {
		
		long time = 0;
		if (ConfigInfo.ACCESSTOKEN != null) {
			time = (System.currentTimeMillis() - ConfigInfo.ACCESSTOKEN.getSaveTime()) / 1000;
		}
		
		this.wxAppId = ConfigInfo.wxAppId;
		this.wxSecret = ConfigInfo.wxSecret;
		this.wxAccessToken = ConfigInfo.ACCESSTOKEN==null ? "null" : ConfigInfo.ACCESSTOKEN.getAccessToken();
		this.wxTokenExpiresIn = ConfigInfo.ACCESSTOKEN==null ? -1 : ConfigInfo.ACCESSTOKEN.getExpiresIn();
		this.wxTokenExcessTime = ConfigInfo.ACCESSTOKEN==null ? -1 : Integer.parseInt((this.wxTokenExpiresIn - time)+"");
		this.accessToken = ConfigInfo.wxAccessToken;
	}
	
	
}
